/*
 *  Copyright (c)  2016-NOW  Jacars Co.Ltd.
 *  All rights reserved.
 */
package com.jacars.biz.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jacars.biz.user.dao.model.JsptApplication;
import com.jacars.biz.user.dao.service.JsptApplicationService;
import com.jacars.biz.user.service.TenantService;
import com.jacars.common.utils.FuncUtil;
import com.jacars.db.support.Condition;
import com.jacars.db.support.Query;
import com.jacars.common.annotation.LoginUser;
import com.jacars.common.constant.CommonConstant;
import com.jacars.common.model.JsptUser;
import com.jacars.common.api.PageResult;
import com.jacars.common.api.Result;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("/app")
@ApiIgnore
@Api(value = "应用管理", tags = "接口")
public class ApplicationController {

	private TenantService tenantService;

	private JsptApplicationService jsptApplicationService;

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "tenantId", value = "参数名称", paramType = "query", dataType = "string"),
			@ApiImplicitParam(name = "tenantName", value = "角色别名", paramType = "query", dataType = "string"),
			@ApiImplicitParam(name = "contactNumber", value = "联系电话", paramType = "query", dataType = "string")
	})
	@ApiOperation(value = "分页", notes = "传入tenant")
	public PageResult<JsptApplication> list(@ApiIgnore @RequestParam Map<String, Object> tenant, Query query, @LoginUser(isFull = true) JsptUser jsptUser) {
		QueryWrapper<JsptApplication> queryWrapper = Condition.getQueryWrapper(tenant, JsptApplication.class);
		IPage<JsptApplication> pages = jsptApplicationService.page(Condition.getPage(query),
				(!jsptUser.getTenantId().equals(CommonConstant.ADMIN_TENANT_ID))
						? queryWrapper.lambda().eq(JsptApplication::getTenantId, jsptUser.getTenantId())
						: queryWrapper);

		return PageResult.succeed(pages.getRecords(), pages.getTotal())	;
		//return PageResult.<JsptApplication>builder()
		//		.code(0)
		//		.msg("操作成功")
		//		.data(pages.getRecords())
		//		.count(pages.getTotal())
		//		.build();
	}


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入tenant")
	public Result<JsptApplication> detail(JsptApplication jsptApplication) {
		JsptApplication detail = jsptApplicationService.getOne(Condition.getQueryWrapper(jsptApplication));
		return Result.succeed(detail);
	}



	/**
	 * 下拉数据源
	 */
	// @GetMapping("/select")
	// @ApiOperation(value = "下拉数据源", notes = "传入tenant")
	// public PageResult<JsptTenant> select(JsptTenant jsptTenant, @LoginUser(isFull = true) JsptUser jsptUser) {
	// 	QueryWrapper<JsptTenant> queryWrapper = Condition.getQueryWrapper(jsptTenant);
	// 	List<JsptTenant> list = jsptApplicationService.list((!jsptUser.getTenantId().equals(CommonConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(JsptTenant::getTenantId, jsptUser.getTenantId()) : queryWrapper);
	//
	// 	return PageResult.<JsptTenant>builder()
	// 			.code(0)
	// 			.msg("操作成功")
	// 			.data(list)
	// 			.count(Long.valueOf(list.size()))
	// 			.build();
	// }

	/**
	 * 自定义分页
	 */
	// @GetMapping("/page")
	// @ApiOperation(value = "分页", notes = "传入tenant")
	// public Result<IPage<JsptTenant>> page(JsptTenant jsptTenant, Query query) {
	// 	IPage<JsptTenant> pages = jsptApplicationService.selectTenantPage(Condition.getPage(query), jsptTenant);
	// 	return Result.succeed(pages);
	// }

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "新增或修改", notes = "传入tenant")
	public Result submit(@Valid @RequestBody JsptApplication jsptApplication) {
		return Result.status(jsptApplicationService.saveOrUpdate(jsptApplication));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public Result remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return Result.status(jsptApplicationService.removeByIds(FuncUtil.toLongList(ids)));
	}

}
